package com.chf.job.controller;

import com.chf.job.common.Const;
import com.chf.job.common.ServerResponse;
import com.chf.job.pojo.Company;
import com.chf.job.pojo.User;
import com.chf.job.service.CompanyService;
import com.chf.job.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class TagController {
    @Autowired
    private TagService tagService;
    @Autowired
    private CompanyService companyService;

    /**
     * 给公司添加标签
     * @param tag
     * @param request
     * @return
     */
    @RequestMapping(value = "tag/addTag.html")
    @ResponseBody
    public ServerResponse addTagByEmail(@RequestParam("tag") String tag, HttpServletRequest request) {
        // 查询用户
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        int result = tagService.addTag(company.getId(), tag);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }

    /**
     * 删除添加的标签
     */
    @RequestMapping(value = "tag/deleteTag.html")
    @ResponseBody
    public ServerResponse deleteTag(@RequestParam("tag") String tag, HttpServletRequest request) {
        User user = (User) request.getSession(true).getAttribute(Const.CURRENT_USER);
        Company company = companyService.getByEmail(user.getEmail());
        int result = tagService.deleteTag(company.getId(), tag);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }
}
